import random

from curl_cffi import requests
from scrapy.http import HtmlResponse
from crawler.connection.tools import retry, joint_url
from scrapy.downloadermiddlewares.retry import RetryMiddleware
from crawler.utils.func_tools import get_tunnel_ip, get_distribute_count

ua_list = [
    "edge99",
    "edge101",
    "chrome99",
    "chrome100",
    "chrome101",
    "chrome104",
    "safari15_3",
    "safari15_5"
]


class CookiesMiddleware(RetryMiddleware):

    @retry(retry_times=4, interval=0)
    def update_request_info(self, request):
        cookies = {
            'language': 'en',
            'country': 'HKG',
            'indicativeCurrency': '',
            'billingCurrency': 'USD',
        }
        num = random.randint(1, 9)
        headers = {
            'authority': 'www.matchesfashion.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'pragma': 'no-cache',
            'sec-ch-ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"macOS"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'none',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3{} (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.3{}".format(
                num, num)
        }

        impersonate = get_distribute_count('impersonate_count', ua_list)

        response = requests.get(url=request.url, headers=headers, proxies=get_tunnel_ip(), timeout=(30, 30),
                                verify=False, impersonate=impersonate, cookies=cookies)
        response = HtmlResponse(request.url, body=response.text, encoding='utf-8', request=request)

        return response

    def process_request(self, request, spider):
        response = self.update_request_info(request)
        return response

    def process_response(self, request, response, spider):
        if "Developer Inactive" in response.text or 'Error code 502' in response.text:
            return self._retry(request, 'Error Request', spider)
        return response
